/* PluginInfo.vala
 *
 * Copyright (C) 2008  Raphael Bosshard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Raphael Bosshard <raphael.bosshard@gmail.com>
 */

using GLib;

public class Appendage.PluginInfo <T> : Object {

	public string			file;
	public string			location;
	public ModuleLoaderType loader_type;
	public ModuleLoaderC <T> module_loader; 

	public string			name;
	public string			description;
	public string			icon_name;
	public string []		authors;
	public string			copyright;
	public string			website;

	private T			plugin;
	public bool			available;
	public bool			enabled;

	/* TODO: Plugin or Module: be coherent in the use of names! chance if
	necessary */

	construct {
		this.location = Appendage.PLUGIN_LOCATION_NOT_SET;
		this.plugin = null;
		this.available = true;
		this.enabled = false;
		this.loader_type = ModuleLoaderType.C;
	}
	
	public void disable () throws Appendage.ModuleError {
		this.enabled = false;
		this.plugin = null;	
		
		return;
	}
	
	/**
	* Returns the plugin object
	*
	*/
	public T get_plugin_object () throws Appendage.ModuleError, Appendage.ModuleLoaderError {
		if (!this.enabled) {
			this.enable ();
		}
		
		return this.plugin;		
	}
	
	/**
	* Initializes a loader according to the plugin's type and tries to create
	* a new module/plugin object
	*/
	public void enable () throws Appendage.ModuleError, Appendage.ModuleLoaderError {
	
		string plugin_path = Path.build_filename (Path.get_dirname (this.file), this.location);

		if (this.enabled && this.plugin != null) {
			GLib.warning ("Plugin '%s' already enabled", plugin_path);
			return;
		}

		switch (this.loader_type) {
			case ModuleLoaderType.C:
				GLib.debug ("Loading plugin module: '%s' using ModuleLoaderC", plugin_path);
				this.module_loader = new Appendage.ModuleLoaderC <T> (plugin_path);
				break;
				
			case ModuleLoaderType.PYTHON:
				this.module_loader = null;
				break;	
		
			default:
				this.module_loader = null;
				break;
		}

		if (this.module_loader == null) {
			GLib.warning ("No module loader found for plugin '%s'", this.location );
			throw new Appendage.ModuleLoaderError.LOADER_NOT_FOUND ("No module loader found for plugin '%s'", this.location);
		}

		this.module_loader.load ();
		this.plugin = this.module_loader.new_object ();

		if (this.plugin == null) {
			GLib.warning ("Module '%s' could not be loaded", this.location);
			throw new Appendage.ModuleError.MODULE_NOT_ENABLED ("Module '%s' could not be loaded", this.location);
		}
	
		// FIXME: Do we really have to keep around a reference ?
		//this.module_loader = null;
		this.enabled = true;		
		return;
	}
}
